package com.dykj.fireFighting.web.app;

import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.file.service.DataFilesService;
import com.dykj.fireFighting.api.base.file.service.OfficeService;
import com.dykj.fireFighting.api.fire.maintenanceplan.dto.RoutineMaintenanceDto;
import com.dykj.fireFighting.api.fire.maintenanceplan.entity.RoutineMaintenance;
import com.dykj.fireFighting.api.fire.maintenanceplan.service.IRoutineMaintenanceService;
import com.dykj.fireFighting.api.fire.maintenanceplan.vo.RoutineMaintenanceVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author fgc
 * @Date 2021/8/5 15:21
 * @ Description：社会端APP查看例行维保记录
 */
@Slf4j
@RestController
@RequestMapping("app/routineMaintenance")
public class AppRoutineMaintenanceController {
    @DubboReference
    private IRoutineMaintenanceService routineMaintenanceService;
    @DubboReference(check = false)
    private OfficeService officeService;
    @DubboReference(check = false)
    private DataFilesService filesService;
    @DubboReference
    private IEnterpriseService enterpriseService;

    /**
     * 例行维保记录列表
     *
     * @return
     */
    /*@AutoLog(value = "例行维保记录-分页列表查询")*/
    @ApiOperation(value = "例行维保记录分页列表", notes = "例行维保记录分页列表")
    @GetMapping("/list")
    // @RequiresPermissions("routineMaintenance/list")
    public CommonResult<PageVo<RoutineMaintenanceVo>> treeGrid(RoutineMaintenanceDto routineMaintenanceDto) {

        routineMaintenanceDto.setSociologyDept(ShiroUtil.getUser().getEnterpriseId().toString());
        PageVo<RoutineMaintenanceVo> page = routineMaintenanceService.queryList(routineMaintenanceDto);
        return CommonResult.success(page);
    }

    /**
     * 获取例行维保记录信息
     */
    @AutoLog(value = "例行维保记录-获取例行维保记录信息")
    @ApiOperation(value = "获取例行维保记录信息", notes = "获取例行维保记录信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    /*@RequiresPermissions("routineMaintenance/info")*/
    public CommonResult<RoutineMaintenance> info(@PathVariable("id") String id) {
        RoutineMaintenance routineMaintenance = routineMaintenanceService.getById(id);
        routineMaintenance.setMaintenanceDeptName(enterpriseService.getById(routineMaintenance.getMaintenanceDept()).getEnterpriseName());
        return CommonResult.success(routineMaintenance);
    }

    /**
     * 添加社会单位评分及意见
     */
    @AutoLog(value = "例行维保记录-添加社会单位评分及意见")
    @ApiOperation(value = "添加社会单位评分及意见", notes = "添加社会单位评分及意见")
    @PostMapping("/evaluate")
    /*@RequiresPermissions("routineMaintenance/info")*/
    public CommonResult update(@Valid @RequestBody RoutineMaintenance routineMaintenance) {
        String name = ShiroUtil.getUser().getName();
        routineMaintenance.setEvaluator(name);
        String ids = "";
        try {
            RoutineMaintenance message = routineMaintenanceService.getRoutineMaintenanceMessage(routineMaintenance.getId());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            message.setMaintenanceDate(sdf.format(message.getCreateDate()));
            message.setSociologyDeptOpinion(routineMaintenance.getSociologyDeptOpinion());
            message.setEvaluator(routineMaintenance.getEvaluator());
            message.setScore(routineMaintenance.getScore());
            message.setSociologyDeptSign(routineMaintenance.getSociologyDeptSign());
            message.setEvaluator(name);
            Map<String, Object> map = new HashMap<>();
            byte[] dataByFileId = filesService.getDataByFileId(routineMaintenance.getSociologyDeptSign());
            map.put("routineMaintenance", message);
            map.put("sign", dataByFileId);
            //map.put("sign","C:\\Users\\Administrator\\Desktop\\16298717336220.png");
            ids = officeService.excelToPdfSave(map, "RoutineMaintenanceRecord.xlsx", message.getSociologyDeptName()+"例行维保报告.pdf");
            //ids = officeService.saveEXceltoMongo(map, null, "RoutineMaintenanceRecord.xlsx", routineMaintenance.getSociologyDeptName() + "例行维保报告.xlsx");
            if(StringUtils.isNotBlank(ids)){
                routineMaintenance.setFileId(ids);
                routineMaintenanceService.updateById(routineMaintenance);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("评价失败");
        }

        return CommonResult.success("评价成功");
    }

}
